<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商家名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入商家名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理商名称" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="套餐类型" prop="packageType">
          <el-select v-model="queryParams.packageType" placeholder="全部">
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="批次号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="操作人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="全部">
          <el-option
            v-for="item in peoplelist"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
       </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="queryParams.orderStatus" placeholder="全部">
            <el-option
              v-for="item in optionsStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="报告结果" prop="orderOutcome">
        <el-select v-model="queryParams.orderOutcome" placeholder="全部">
          <el-option
            v-for="item in optionsOutcome"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <!-- <el-input v-model="form2.orderOutcome" placeholder="请输入代理商id" /> -->
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker style="width: 210px;" v-model="daterangeCreateTime" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
    </el-form>
    <div class="two-buttons">
      <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">查询</el-button>
      <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
    </div>
    <el-divider></el-divider>
    <el-table v-loading="loading" :data="orderParentList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="id" width="80" />
      <el-table-column label="商家名称" align="center" prop="storeName" />
      <el-table-column label="所属代理商名称" align="center" prop="agentName" />
      <el-table-column label="批次号" align="center" prop="number" />
      <el-table-column label="套餐类型" align="center" prop="packageType" width="200">
        <template slot-scope="scope">
          <p v-if="scope.row.packageType === '0'">线上</p>
          <p v-if="scope.row.packageType === '1'">线下</p>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <p v-if="scope.row.payStatus == '0'">未支付</p>
          <p v-if="scope.row.payStatus == '1'">已支付</p>
        </template>
      </el-table-column>
      <el-table-column label="背调人数" align="center" prop="cdNumber" />
      <el-table-column label="总金额" align="center" prop="amount" />
      <el-table-column label="操作人" align="center" prop="userName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="200" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['orderParent:orderParent:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改批量订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderParent, getOrderParent, delOrderParent, addOrderParent, updateOrderParent } from "@/api/orderParent/orderParent";
import { listMyUser} from "@/api/system/user";
export default {
  name: "OrderParent",
  data() {
    return {
      peoplelist: [],
      daterangeCreateTime: [],
      optionsStatus: [{
        value: 0,
        label: '未开始'
      }, {
        value: 1,
        label: '进行中'
      }, {
        value: 2,
        label: '已完成'
      }, {
        value: 3,
        label: '异常'
      }],
      optionsOutcome: [{
        value: 1,
        label: '待出具'
      }, {
        value: 2,
        label: '无风险'
      }, {
        value: 3,
        label: '低风险'
      }, {
        value: 4,
        label: '中风险'
      },{
        value: 5,
        label: '高风险'
      }],
      typeList: [{
        value: '0',
        label: '线上'
      }, {
        value: '1',
        label: '线下'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysOrder: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 批量订单表格数据
      orderParentList: [],
      // ${subTable.functionName}表格数据
      sysOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        agentName: null,
        packageType: null,
        number: null,
        userId: null,
        orderStatus: null,
        orderOutcome: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getPeoplelist();
  },
  methods: {
    // 拿到操作人列表
    getPeoplelist() {
      listMyUser().then(res => {
        this.peoplelist = res.data;
      })
    },
    /** 查询批量订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listOrderParent(this.queryParams).then(response => {
        this.orderParentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeName: null,
        agentName: null,
        packageType: null,
        number: null,
        userId: null,
        orderStatus: null,
        orderOutcome: null
      };
      this.sysOrderList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加批量订单";
    },
    /** 查看按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: "/orderParent/ordersDetails", query: { id: row.id || this.ids } });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysOrderList = this.sysOrderList;
          if (this.form.id != null) {
            updateOrderParent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderParent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除批量订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrderParent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** ${subTable.functionName}序号 */
    rowSysOrderIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddSysOrder() {
      let obj = {};
      obj.number = "";
      obj.cdName = "";
      obj.idNumber = "";
      obj.phone = "";
      obj.certificate = "";
      obj.amount = "";
      obj.authorizationType = "";
      obj.payStatus = "";
      obj.orderStatus = "";
      obj.orderOutcome = "";
      obj.storeId = "";
      obj.storeName = "";
      obj.agentName = "";
      obj.reportUrl = "";
      obj.setmealId = "";
      obj.remark = "";
      obj.userId = "";
      obj.deptId = "";
      this.sysOrderList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteSysOrder() {
      if (this.checkedSysOrder.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const sysOrderList = this.sysOrderList;
        const checkedSysOrder = this.checkedSysOrder;
        this.sysOrderList = sysOrderList.filter(function(item) {
          return checkedSysOrder.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSysOrderSelectionChange(selection) {
      this.checkedSysOrder = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orderParent/orderParent/export', {
        ...this.queryParams
      }, `orderParent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
